﻿using System.Collections;
using System.Collections.Generic;

namespace Fabrication.DataStructures
{
    public class Sequence<T> : IEnumerable<T>
    {
        private int index = -1;
        private readonly List<T> queue = new List<T>();

        public Sequence(params T[] items)
        {
            foreach (var item in items)
                Add(item);
        }

        public Sequence(IEnumerable<T> items)
        {
            foreach (var item in items)
                Add(item);
        }

        public void Add(T item)
        {
            queue.Add(item);
        }

        public T Next()
        {
            if (queue.Count == 0)
                throw new EmptySequenceException("The sequence must be populated before calling 'Next'.");

            index++;
            if (index == queue.Count)
                index = 0;

            return queue[index];
        }

        public IEnumerator<T> GetEnumerator()
        {
            return queue.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return queue.GetEnumerator();
        }
    }
}
